package sqlist;

import java.util.Arrays;

public class MyArraylist {
    public int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 4;

    public MyArraylist(){
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 打印顺序表 运用usedsize
     */

    public void disply(){
        for(int i = 0; i < this.usedSize; i ++){
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    public boolean isFull(){
        if(this.usedSize == this.elem.length){
            return true;
        }
        return false;
    }

    public boolean checkPosInAdd(int pos){
        if(pos < 0 || pos > this.usedSize){
            System.out.println("pos的位置不合法");
            return false;
        }
        return true;
    }

    public void add(int pos, int data){
        //usedSize是可以放数据的
        if(!checkPosInAdd(pos)){
            throw new MyArrayListIndexOutOfException("添加方法的pos不合理");
        }
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem, this.elem.length *2);
        }

        for(int i = this.usedSize - 1; i >= pos; i--){
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize ++;
    }

    public void add(int data){
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem, this.elem.length * 2);
        }
        this.elem[usedSize] = data;
        this.usedSize ++;
    }

    public boolean contain(int toFind){
        for (int i = 0; i < this.usedSize;i ++){
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    //找到某元素的对应位置；
    public int indexOf(int data){
        for(int i = 0; i < this.usedSize; i++){
            if(this.elem[i] == data){
                return i;
            }
        }
        return -1;
    }
    private boolean checkPosInGet(int pos){
        if(pos < 0 || pos >= this.usedSize){
            System.out.println("pos位置不合法");
            return false;
        }
        return true;
    }
    public boolean isEmpty(){
        return this.usedSize == 0;
    }
    //获取pos位置的元素
    public int get(int pos){
        if (!checkPosInGet(pos)){
            throw new MyArrayListIndexOutOfException("获取pos位置下标是，位置不合法");
        }
        if (isEmpty()){
            throw new MyArrayListEmptyException("获取元素的时候，顺序表为空");

        }
        return this.elem[pos];
    }

    //给pos位更新宸value
    public void set(int pos, int value){
        if (!checkPosInGet(pos)){
            throw new MyArrayListIndexOutOfException("设置位置不合法");
        }
        if(isEmpty()){
            throw new MyArrayListEmptyException("设置元素的的时候，顺序表为空");
        }
        this.elem[pos] = value;

    }
    //删除第一次出现的关键字key
    public void remove(int key){
        if(isEmpty()){
            throw new MyArrayListEmptyException("顺序表为空，不能删除");
        }
        int index = indexOf(key);
        for(int i = index; i < usedSize-1; i++) {
            this.elem[i] = this.elem[i + 1];
        }
        this.usedSize--;
    }

    public int size(){
        return this.usedSize;
    }
    public void clear(){
        this.usedSize = 0;
    }
}
